package com.example.mobilesafe.clean.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mobilesafe.R;
import com.example.mobilesafe.clean.entity.RubbishInfo;

import java.text.DecimalFormat;
import java.util.List;

public class RubbishListAdapter extends BaseAdapter {
    private Context context;
    private List<RubbishInfo> rubbishInfos;
    public RubbishListAdapter(Context context,List<RubbishInfo> rubbishInfos){
        super();
        this.context = context;
        this.rubbishInfos = rubbishInfos;//接受传递过来的列表数据
    }
    @Override
    public int getCount() {
        return rubbishInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return rubbishInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_list_rubbish_clean,null);
            holder.mAppIconImgv = convertView.findViewById(R.id.iv_appicon_rubbishclean);
            holder.mAppNameTv = convertView.findViewById(R.id.tv_appname_rubbishclean);
            holder.mRubbishSizeTv = convertView.findViewById(R.id.tv_appsize_rubbishclean);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        RubbishInfo rubbishInfo = rubbishInfos.get(position);
        holder.mAppIconImgv.setImageDrawable(rubbishInfo.appIcon);
        holder.mAppNameTv.setText(rubbishInfo.appName);
        //设置软件垃圾大小，此处使用的FormatFileSize()方法在后续创建
        holder.mRubbishSizeTv.setText(FormatFileSize(rubbishInfo.rubbishSize));
        return convertView;
    }
    static class ViewHolder{
        ImageView mAppIconImgv;
        TextView mAppNameTv;
        TextView mRubbishSizeTv;
    }

    public static String FormatFileSize(long fileS){
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileS == 0){
            return wrongSize;
        }
        if (fileS < 1024){
            fileSizeString = df.format((double) fileS) + "b";
        }else if (fileS < 1048576){
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        }else if (fileS < 1073741824){
            fileSizeString = df.format((double) fileS / 1048576) + "M";
        }else {
            fileSizeString = df.format((double) fileS / 1073741824) + "G";
        }
        return fileSizeString;
    }
}
